package com.apobates.jforum2.threads.stub.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * 话题
 */
public class ThreadsVo implements Serializable{
    //话题ID
    private long id;
    //主题
    private String title;
    //发布日期
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime entryDateTime;
    //作者:会员的ID
    private long member;
    private String nickname;
    private String ipAddr;
    //卷的ID
    private int volumes;
    //版块的ID
    private long board;
    //置顶(时效),true(1)置顶,false(0)不置顶
    private boolean tops;
    //加精,true(1)精华,false(0)非精华
    private boolean goods;
    //火贴,true(1)火,false(0)没火
    private boolean hots;
    //是否有像册@20190412
    private long album;
    //状态
    private int status;
    private String statusTitle;
    //显示顺序:普通话题是1,置顶的依次++
    private int ranking;
    //排序的日期
    //没有回复时为entryDateTime
    //有回复时为最近的回复日期
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime rankingDateTime;
    //话题类型的参数名,年代
    private String categoryName;
    //话题类型的参数值,year
    private String categoryValue;
    //简述|举报和投拆不要设置此值
    private String summary;
    //是否匿名,true是,false不是
    private boolean anony;
    //是今日的话题吗
    private boolean today;
    //访问连接
    private String link;
    private final static Pattern TOPIC_URL_PARSE = Pattern.compile("/topic/(.*).xhtml");

    public ThreadsVo() {
    }

    public long getMember() {
        return member;
    }

    public void setMember(long member) {
        this.member = member;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getVolumes() {
        return volumes;
    }

    public void setVolumes(int volumes) {
        this.volumes = volumes;
    }

    public long getBoard() {
        return board;
    }

    public void setBoard(long board) {
        this.board = board;
    }

    public long getAlbum() {
        return album;
    }

    public void setAlbum(long album) {
        this.album = album;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public LocalDateTime getEntryDateTime() {
        return entryDateTime;
    }

    public void setEntryDateTime(LocalDateTime entryDateTime) {
        this.entryDateTime = entryDateTime;
    }

    public String getIpAddr() {
        return ipAddr;
    }

    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }

    public boolean isTops() {
        return tops;
    }

    public void setTops(boolean tops) {
        this.tops = tops;
    }

    public boolean isGoods() {
        return goods;
    }

    public void setGoods(boolean goods) {
        this.goods = goods;
    }

    public boolean isHots() {
        return hots;
    }

    public void setHots(boolean hots) {
        this.hots = hots;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getStatusTitle() {
        return statusTitle;
    }

    public void setStatusTitle(String statusTitle) {
        this.statusTitle = statusTitle;
    }

    public int getRanking() {
        return ranking;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    public LocalDateTime getRankingDateTime() {
        return rankingDateTime;
    }

    public void setRankingDateTime(LocalDateTime rankingDateTime) {
        this.rankingDateTime = rankingDateTime;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public String getCategoryValue() {
        return categoryValue;
    }

    public void setCategoryValue(String categoryValue) {
        this.categoryValue = categoryValue;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public boolean isAnony() {
        return anony;
    }

    public void setAnony(boolean anony) {
        this.anony = anony;
    }

    public boolean isToday() {
        return today;
    }

    public void setToday(boolean today) {
        this.today = today;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }
    @JsonIgnore
    public boolean isNormal() {
        return 4 == status;
    }

    public static Optional<ThreadsVo> parseLink(String link){
        String[] params = {};
        try {
            params = link.split("-");
        } catch (NullPointerException | PatternSyntaxException e) {
            //throw new IllegalStateException("构造话题的ActiveDirectoryConnector实例参数不合法");
        }
        if (params.length == 0) {
            //throw new IllegalStateException("构造话题的ActiveDirectoryConnector实例失败");
            return Optional.empty();
        }
        //话题: x-x-x
        //版块: x-x
        //版块组: x
        if (params.length == 1) {
            ThreadsVo topic = new ThreadsVo();
            //topic.setVolumesId(Commons.stringToInteger(params[0], -1));
            int volumesId = -1;
            try{
                volumesId = Integer.valueOf(params[0]);
            }catch (NumberFormatException e){}
            topic.setVolumes(volumesId);
            return Optional.of(topic);
        }
        if (params.length == 2) {
            ThreadsVo topic = new ThreadsVo();
            //topic.setBoardId(Commons.stringToLong(params[0], 0L));
            //topic.setVolumesId(Commons.stringToInteger(params[1], -1));
            long boardId=0L;
            try{
                boardId = Long.valueOf(params[0]);
            }catch(NumberFormatException e){}
            topic.setBoard(boardId);
            //
            int volumesId = -1;
            try{
                volumesId = Integer.valueOf(params[1]);
            }catch (NumberFormatException e){}
            topic.setVolumes(volumesId);
            return Optional.of(topic);
        }
        if (params.length == 3) {
            ThreadsVo topic = new ThreadsVo();
            //topic.setId(Commons.stringToLong(params[0], 0L));
            //topic.setBoardId(Commons.stringToLong(params[1], 0L));
            //topic.setVolumesId(Commons.stringToInteger(params[2], -1));
            long id=0L;
            try{
                id = Long.valueOf(params[0]);
            }catch(NumberFormatException e){}
            topic.setId(id);
            //
            long boardId=0L;
            try{
                boardId = Long.valueOf(params[1]);
            }catch(NumberFormatException e){}
            topic.setBoard(boardId);
            //
            int volumesId = -1;
            try{
                volumesId = Integer.valueOf(params[2]);
            }catch (NumberFormatException e){}
            topic.setVolumes(volumesId);
            return Optional.of(topic);
        }
        return Optional.empty();
    }
    public static Optional<ThreadsVo> parseLink(String requestRefString, String siteDomain) {
        if (requestRefString == null || !requestRefString.startsWith(siteDomain)) { // 只允许本站连接
            return Optional.empty();
        }
        //
        Matcher m = TOPIC_URL_PARSE.matcher(requestRefString);
        String connectValue = null;
        if (m.find()) {
            connectValue = m.group(1);
        }
        return parseLink(connectValue);
    }
}